package com.sl.core.engine.check;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.hop.core.CheckResult;
import org.apache.hop.core.ICheckResult;
import org.apache.hop.core.ICheckResultSource;

import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ProcessCompCheckResult extends CheckResult implements ICheckResult {

    private String stepCode;

    public static ProcessCompCheckResult normalErrorMessage(String stepCode, String message) {
        ProcessCompCheckResult processCompCheckResult = new ProcessCompCheckResult();
        processCompCheckResult.setStepCode(stepCode);
        processCompCheckResult.setText(message);
        processCompCheckResult.setType(ICheckResult.TYPE_RESULT_ERROR);
        processCompCheckResult.setErrorCode("501");
        return processCompCheckResult;
    }

    public static List<ProcessCompCheckResult> of(List<ICheckResult> collect) {
        return collect.stream().map(v -> {
            if (v instanceof ProcessCompCheckResult vp) {
                return vp;
            }
            int type = v.getType();
            String errorCode = v.getErrorCode();
            if (StrUtil.isBlank(errorCode)) {
                errorCode = "500";
            }
            ProcessCompCheckResult processCompCheckResult = new ProcessCompCheckResult();
            processCompCheckResult.setType(type);
            processCompCheckResult.setText(v.getText());
            processCompCheckResult.setErrorCode(errorCode);

            ICheckResultSource sourceInfo = v.getSourceInfo();
            if (ObjectUtil.isNotNull(sourceInfo)) {
                String name = sourceInfo.getName();
                processCompCheckResult.setStepCode(name);
            }
            return processCompCheckResult;
        }).collect(Collectors.toList());
    }

//    int TYPE_RESULT_NONE = 0;
//
//    int TYPE_RESULT_OK = 1;
//
//    int TYPE_RESULT_COMMENT = 2;
//
//    int TYPE_RESULT_WARNING = 3;
//
//    int TYPE_RESULT_ERROR = 4;

}
